package com.balloon.springboot.job.web;

import com.balloon.springboot.core.rules.DatePageVO;
import com.balloon.springboot.core.rules.ResultVO;
import com.balloon.springboot.core.rules.ResultVOUtils;
import com.balloon.springboot.quartz.dto.QuartzJobDto;
import com.balloon.springboot.quartz.service.JobService;
import com.balloon.springboot.quartz.vo.QuartzJobVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 自动任务控制器
 *
 * @author liaofuxing
 */
@RestController
@RequestMapping("/job")
public class JobController {
    private final static Logger logger = LoggerFactory.getLogger(JobController.class);

    @Autowired
    private JobService jobService;

    /**
     * 新增任务
     *
     * @param quartz 任务对象
     * @return ResultVO<Object>
     * @author liaofuxing
     */
    @PostMapping("/addJob")
    public ResultVO<Object> save(@RequestBody QuartzJobDto quartz) {
        logger.info("新增任务");
        jobService.saveJob(quartz);
        return ResultVOUtils.success(null);
    }

    /**
     * 修改任务
     *
     * @param quartz 任务对象
     * @return ResultVO<Object>
     * @author liaofuxing
     */
    @PostMapping("/editJob")
    public ResultVO<Object> edit(@RequestBody QuartzJobDto quartz) {
        logger.info("编辑任务");
        jobService.updateJob(quartz);
        return ResultVOUtils.success(null);
    }

    /**
     * 任务列表不分页
     *
     * @param quartzJobDto 任务对象
     * @return ResultVO<DatePageVO < QuartzJobVo>>
     */
    @GetMapping("/findJobList")
    public ResultVO<DatePageVO<QuartzJobVo>> list(QuartzJobDto quartzJobDto) {
        logger.info("任务列表");
        DatePageVO<QuartzJobVo> quartzJobPage = jobService.quartzJobPage(quartzJobDto);
        return ResultVOUtils.success(quartzJobPage);
    }

    /**
     * 触发任务
     *
     * @param quartzJobDto
     * @return ResultVO<Object>
     * @author liaofuxing
     */

    @PostMapping("/trigger")
    public ResultVO<Object> trigger(@RequestBody QuartzJobDto quartzJobDto) {
        logger.info("触发任务");
        jobService.triggerJob(quartzJobDto.getJobName(), quartzJobDto.getJobGroup());
        return ResultVOUtils.success(null);
    }

    /**
     * 停止或恢复任务
     *
     * @param quartzJobDto 任务Dto
     * @return ResultVO<Object>
     */
    @PostMapping("/runOrStopJob")
    public ResultVO<DatePageVO<QuartzJobVo>> runOrStopJob(@RequestBody QuartzJobDto quartzJobDto) {
        jobService.runOrStopJob(quartzJobDto);
        return ResultVOUtils.success(null);
    }

    /**
     * 移除任务
     *
     * @param quartzJobDto 任务Dto
     * @return ResultVO<Object>
     * @author liaofuxing
     */
    @PostMapping("/removeJob")
    public ResultVO<Object> remove(@RequestBody QuartzJobDto quartzJobDto) {
        logger.info("移除任务");
        jobService.removeJob(quartzJobDto.getJobName(), quartzJobDto.getJobGroup());
        return ResultVOUtils.success(null);
    }

    /**
     * 校验用户名重复
     *
     * @param jobName 用户名
     * @param id      用户id
     * @return ResultVO
     * @required username
     * @required id
     */
    @GetMapping("/validateJobNameRepeat")
    public ResultVO<Boolean> validateJobNameRepeat(String jobName, Integer id) {
        Boolean validate = jobService.validateJobNameRepeat(jobName, id);
        return ResultVOUtils.success(validate);
    }

    /**
     * 停止任务,  统一使用 runOrStopJob 方法
     *
     * @param jobName  任务名
     * @param jobGroup 任务组
     * @return ResultVO<Object>
     * @author liaofuxing
     */
    @Deprecated
    @PostMapping("/pause")
    public ResultVO<Object> pause(String jobName, String jobGroup) {
        logger.info("停止任务");
        jobService.pauseJob(jobName, jobGroup);
        return ResultVOUtils.success(null);
    }

    /**
     * 恢复任务, 废弃, 统一使用 runOrStopJob 方法
     *
     * @param jobName  任务名
     * @param jobGroup 任务组
     * @return ResultVO<Object>
     * @author liaofuxing
     */
    @Deprecated
    @PostMapping("/resume")
    public ResultVO<Object> resume(String jobName, String jobGroup) {
        logger.info("恢复任务");
        jobService.resumeJob(jobName, jobGroup);
        return ResultVOUtils.success(null);
    }

}
